package com.example.myapplication.Fragment;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.Adapter.commityApaer;
import com.example.myapplication.Bean.communityBean;
import com.example.myapplication.R;
import com.example.myapplication.mySQL.mySQL;

import java.util.ArrayList;
import java.util.List;

public class CommunityFragment extends Fragment {
    TextView text;
    ListView lvchihu;
    List<communityBean> list = new ArrayList<>();
    commityApaer adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_community_fragment, null);
        lvchihu = view.findViewById(R.id.LV_community);
        text=view.findViewById(R.id.tv_title);
        text.setText("吃货驾到");
        list.add(new communityBean((R.drawable.p1_gourmet), "减肥干嘛 又不是吃不起", "叶德娴", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p2_gourmet), "这样的馒头，感觉能吃一筐", "刘芸", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p3_gourmet), "给你一个爱上烘培的理由", "徐自贤", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p4_gourmet), "不是我瘦不下来 是敌人太强", "丁志诚", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p5_gourmet), "一场咖啡与鲜花的比赛", "", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p6_gourmet), "美食是灵魂的伴侣", "张笛", "2023-05-15", "0"));
        list.add(new communityBean((R.drawable.p7_gourmet), "吃货的幸福世界", "杨若兮", "2023-05-15", "0"));

        adapter = new commityApaer(list, getActivity(), 1);
        //4、ListView设置适配器
        lvchihu.setAdapter(adapter);
        //注册长按菜单
        registerForContextMenu(lvchihu);
        return view;
    }

    //添加菜单
    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, Menu.NONE, "收藏");
        menu.add(0, 2, Menu.NONE, "删除");
    }

    //添加点击事件
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //绑定数据
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case 1:
                //初始化MysqlHelper
                mySQL mysqlHelper=new mySQL(getActivity(),"shoucang",null, 2);

                //获取mysql对象
                SQLiteDatabase sqLiteDatabase=mysqlHelper.getWritableDatabase();
                //初始化数据
                //添加
                Log.i("1","1");
                ContentValues contentValues=new ContentValues();

                contentValues.put("img",list.get(info.position).getImg());
                contentValues.put("name",list.get(info.position).getName());
                contentValues.put("title",list.get(info.position).getBiaoti());
                contentValues.put("date",list.get(info.position).getDate());
                contentValues.put("count",list.get(info.position).getNum());
                Log.i("1",list.get(info.position).getImg() + list.get(info.position).getName());
                sqLiteDatabase.insert("shoucang",null,contentValues);

                Toast.makeText(getActivity(), "选择了收藏", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                //添加删除事件
                list.remove(info.position);
                //刷新视图
                adapter.notifyDataSetInvalidated();
                Toast.makeText(getActivity(), "选择了删除", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }
}